package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Doctor;
import com.kkb.service.DoctorService;
import com.kkb.utils.ResultUtil;
import com.kkb.vo.DoctorVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author:CoolTiger
 * @title: DoctorController
 * @projectName ssm_hospital
 * @description: TODO 医生Controller层
 * @date:2022/07/09/16:32:02
 **/
@Controller
@ResponseBody
@RequestMapping("/hospital/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    /**
     * TODO 医生多条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultUtil<Doctor> queryAll(Integer pageNum, Integer pageSize, DoctorVo vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Doctor> doctorPageInfo = doctorService.queryAll(pageNum, pageSize, vo);
        return new ResultUtil<Doctor>(doctorPageInfo);
    }
    //添加
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultUtil<Doctor> addDoctor(Doctor doctor) {
        int i = doctorService.addDoctor(doctor);
        if (i == 1) {
            return new ResultUtil<Doctor>(doctor);
        }
        return new ResultUtil<>(500,"添加失败");
    }
    //删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultUtil<Doctor> deleteDoctor(@PathVariable("id") Integer doctorId){
        int i = doctorService.deleteDoctor(doctorId);
        if (i == 1){
            return new ResultUtil<>(200,"删除成功");
        }
        return new ResultUtil<>(500,"删除失败");
    }
    //信息回显
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultUtil<Doctor> queryById(@PathVariable("id") Integer doctorId){
        System.out.println(doctorId);
        Doctor doctor = doctorService.queryById(doctorId);
        if (doctor != null){
            return new ResultUtil<>(doctor);
        }
        return new ResultUtil<>(500,"信息不存在");
    }
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultUtil<Doctor> updateDoctor(Doctor doctor,@PathVariable("id") Integer doctorId){
        doctor.setdId(doctorId);
        int i = doctorService.updateDoctor(doctor);
        if (i == 1){
            return new ResultUtil<>(doctor);
        }
        return new ResultUtil<>(500,"更新失败");
    }
}
